package com.itboye.jinpuyiyuan.activity.me;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.UserInfoBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 实名认证
 */
public class RealNameActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private EditText identifyEt,nameEt;
    private TextView real_ok,changeNumTv;
    private BranchAddressPresenter presenter,presenter1;
    private Intent intent;

    private String uid;
    private int limit;//一天限制修改的次数
    private int count=0;//记录修改次数
    private long date=0l;//上次提交时间
    private long dateCha=0l;//上次提交时间与这次的时间差
    private String realName,idCard;
    private PopupWindow surePop,canclePop;
    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_name);
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("实名认证");



        intent=getIntent();
//        timer=new Timer();
//
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                if (canclePop.isShowing()){
//                    canclePop.dismiss();
//                }
//            }
//        },300);

        presenter=new BranchAddressPresenter(this);
        presenter1=new BranchAddressPresenter(this);
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");
        limit= (int) SPUtils.get(getApplication(),null,"limit",1);

        changeNumTv.setText("一天限制修改次数为"+limit+"次");
        presenter.getPersonInfo(uid);



    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

            case R.id.real_ok:

//                if (dateCha<24*60*60*1000&&dateCha>0){//这是一天中的第二次修改
//
//                }
                int limitCount= (int) SPUtils.get(getApplication(),uid,"limit_count",0);
                long limitDate = (long) SPUtils.get(getApplication(),uid,"limit_date",0l);
                Log.d("dateCha",limitCount+"=="+limitDate);
                if(limitCount==2&&limitCount!=0&&limitDate!=0){
                    dateCha=System.currentTimeMillis()-limitDate;
                    Log.d("dateCha",dateCha+"");
                    if (dateCha>24*60*60*1000){
                        SPUtils.put(RealNameActivity.this,uid,"limit_count",0);
                        if (identifyEt.getText().toString().equals("")
                                ||nameEt.getText().toString().equals("")){
                            MAlert.alert("请填写完信息");
                        }else if (realName.equals(nameEt.getText().toString())&&idCard.equals(identifyEt.getText().toString())){
                            MAlert.alert("信息未修改，请修改后在提交");
                        }else{
                            showDialog1();

                        }
                    }else if (limitCount==2){
                        showDialog2();
                    }

                }else {
                    if (identifyEt.getText().toString().equals("")
                            ||nameEt.getText().toString().equals("")){
                        MAlert.alert("请填写完信息");
                    }else if (realName!=null&&realName.equals(nameEt.getText().toString())&&idCard!=null&&idCard.equals(identifyEt.getText().toString())){
                        MAlert.alert("信息未修改，请修改后在提交");
                    }else{
                        showDialog1();

                    }
                }


                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity resultEntity = handlerError(arg);

        if (resultEntity!=null){

            if (resultEntity.getEventType()== BranchAddressPresenter.By_UserIdCard_info_success){//获取用户身份信息
              UserInfoBean user= (UserInfoBean) resultEntity.getData();
              realName=user.getReal_name();
              idCard=user.getCard_no();
//              MAlert.alert("获取用户信息成功");
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_UserIdCard_info_fail){

//                MAlert.alert("获取用户信息失败");
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_UserIdCard_update_success){//更改用户身份信息
//                MAlert.alert("数据保存成功");
                SPUtils.put(RealNameActivity.this,uid,"isVerify",true);
                if ((int)SPUtils.get(getApplication(),uid,"limit_count",0)!=0){
                    count= (int) SPUtils.get(getApplication(),uid,"limit_count",0);
                }
                count++;
                SPUtils.put(RealNameActivity.this,uid,"limit_count",count);
                if (count==1){
                    date=System.currentTimeMillis();
                    SPUtils.put(RealNameActivity.this,uid,"limit_date",date);
                }

//                if (date==0){
//
//                }else {
//                    dateCha=System.currentTimeMillis()-date;
//                }

                setResult(200,intent.putExtra("isSuccess",true));
                RealNameActivity.this.finish();
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_UserIdCard_update_fail){
//                MAlert.alert("数据保存失败");
                setResult(200,intent.putExtra("isSuccess",false));
            }


        }
    }

    public void showDialog1(){

        View convertView = LayoutInflater.from(this).inflate(R.layout.dialog_realname, null);
        surePop=new PopupWindow(convertView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        surePop.setAnimationStyle(R.style.popupwindow_anim_style);
        surePop.setFocusable(true);
        TextView idCardTv=convertView.findViewById(R.id.idCardTv);
        TextView nameTv= convertView.findViewById(R.id.nameTv);
        idCardTv.setText(identifyEt.getText().toString());
        nameTv.setText(nameEt.getText().toString());
        final TextView sureTv=convertView.findViewById(R.id.sureTv);
        TextView cancelTv=convertView.findViewById(R.id.cancelTv);
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                surePop.dismiss();
                presenter1.updatePersonInfo(uid,identifyEt.getText().toString(),nameEt.getText().toString());
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                surePop.dismiss();
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                surePop.dismiss();
            }
        });
        convertView.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        convertView.setFocusableInTouchMode(true); //comment by
        surePop.showAtLocation(convertView, Gravity.TOP,0,0);



    }

    //显示限制次数的pop
    public void showDialog2(){

        View convertView = LayoutInflater.from(this).inflate(R.layout.popwind_realname_limit, null);
        canclePop=new PopupWindow(convertView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        canclePop.setAnimationStyle(R.style.popupwindow_anim_style);
        canclePop.setFocusable(true);

        TextView limitTv= convertView.findViewById(R.id.limitTv);
        limitTv.setText("一天只能修改"+limit+"次");

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                canclePop.dismiss();
            }
        });

        convertView.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        convertView.setFocusableInTouchMode(true); //comment by
        canclePop.showAtLocation(convertView, Gravity.TOP,0,0);

    }
    public int getSexInt(String s){
        if ("男".equals(s.trim())){
            return 1;
        }else if ("女".equals(s.trim())){
            return 0;
        }
        return 2;
    }

    @Override
    public void finish() {
        super.finish();
    }
}
